/*
 * 
 * 
 * 
 */
package com.hboxs.asl.controller.admin;

import com.hboxs.asl.Filter;
import com.hboxs.asl.Message;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.entity.Merchant;
import com.hboxs.asl.entity.Withdrawal;
import com.hboxs.asl.service.MerchantService;
import com.hboxs.asl.service.WithdrawalService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * Controller - 提现记录
 */
@Controller("adminWithdrawalMerchantController")
@RequestMapping("/asl-admin/withdrawal_merchant")
public class WithdrawalMerchantController extends BaseController {

    @Resource(name = "withdrawalServiceImpl")
    private WithdrawalService withdrawalService;
    @Resource(name = "merchantServiceImpl")
    private MerchantService merchantService;

    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model, HttpSession session) {
        return "/admin/withdrawal_merchant/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(Withdrawal withdrawal, RedirectAttributes redirectAttributes) {
        if (!isValid(withdrawal)) {
            return ERROR_VIEW;
        }
        withdrawalService.save(withdrawal);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model, HttpSession session) {
        model.addAttribute("withdrawal", withdrawalService.find(id));
        return "/admin/withdrawal_merchant/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Long id , Withdrawal.Status status ,String rejectReason ,  RedirectAttributes redirectAttributes) {

        Withdrawal withdrawal = withdrawalService.find(id);
        Merchant merchant = withdrawal.getMerchant();

        // 拒绝将返还积分（商家）
        if(merchant!=null && status==Withdrawal.Status.reject){
            merchant.setBalance(merchant.getBalance().add(withdrawal.getPoint()));
            merchantService.update(merchant);
            withdrawal.setRejectReason(rejectReason);
        }

        withdrawal.setStatus(status);
        withdrawalService.update(withdrawal);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:edit.htm?id="+id;
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Pageable pageable, ModelMap model) {
        pageable.getFilters().add(Filter.isNotNull("merchant"));
        pageable.getFilters().add(Filter.isNull("member"));
        model.addAttribute("page", withdrawalService.findPage(pageable));
        return "/admin/withdrawal_merchant/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Message delete(Long[] ids) {
        withdrawalService.delete(ids);
        return SUCCESS_MESSAGE;
    }

}
